#include <QDebug>
#include <QMessageBox>
#include "FindInProjectDlg.h"
#include "ui_FindInProjectDlg.h"
#include "Include/Project.h"

FindInProjectDlg::FindInProjectDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FindInProjectDlg)
{
    ui->setupUi(this);

    ui->edtSearch->setText(Plugins::Project::currentInstance()->lastSearch());
    ui->edtFilePattern->setText(Plugins::Project::currentInstance()->lastSearchFilePattern());

    int flags = Plugins::Project::currentInstance()->searchFlags();
    ui->chkCase->setChecked(flags & 1);
    ui->chkWhole->setChecked(flags & 2);
    ui->chkRegular->setChecked(flags & 4);

    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccept()));
}

FindInProjectDlg::~FindInProjectDlg()
{
    delete ui;
}

void FindInProjectDlg::onAccept()
{
    Plugins::Project::currentInstance()->setLastSearch(ui->edtSearch->text());
    Plugins::Project::currentInstance()->setLastSearchFilePattern(ui->edtFilePattern->text());
    int flags = 0;
    if (ui->chkCase->isChecked())
        flags |= 1;
    if (ui->chkWhole->isChecked())
        flags |= 2;
    if (ui->chkRegular->isChecked())
        flags |= 4;
    Plugins::Project::currentInstance()->setSearchFlags(flags);

    if (ui->edtSearch->text().isEmpty()){
        QMessageBox::warning(this, tr("Error"), tr("Emtpy search string"));
        return;
    }
    if (ui->edtFilePattern->text().isEmpty()){
        QMessageBox::warning(this, tr("Error"), tr("Emtpy file pattern string"));
        return;
    }
    accept();
}

QString FindInProjectDlg::search()
{
    return ui->edtSearch->text();
}

QString FindInProjectDlg::filePattern()
{
    return ui->edtFilePattern->text();
}

bool FindInProjectDlg::isCase()
{
    return ui->chkCase->isChecked();
}

bool FindInProjectDlg::isWhole()
{
    return ui->chkWhole->isChecked();
}

bool FindInProjectDlg::isRegular()
{
    return ui->chkRegular->isChecked();
}

